// pages/timetable/timetable.js
const app = getApp();
const plugin = requirePlugin("edu-core");
const {
  devMode,
  appId
} = require('../../index')
const emitter = plugin.emitter;
const {
  classTime
} = require('../../config');
const {
  buildDayTable,
  sortDayTable
} = require('../../utils/handler');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    classTime,
    courseData: [],
    //预览信息
    viewShow: false,
    viewCourse: {},
    //用户ID
    userId: ''
  },
  addTable(e) {
    let id = e.detail;
    console.log(id)
    let day = parseInt(id % 7);
    let nums = parseInt(id / 7);
    wx.navigateTo({
      url: "plugin://edu-core/custom-page?day=" + day + '&nums=' + nums + '&userId=' + this.data.userId
    });
  },
  //周次修改
  weekChange(e) {
    //  console.log(e)
    this.setData({
      week: e.detail,
      courseData: []
    }, () => {
      this.queryTimeTable({
        week: e.detail
      })
      this.queryCustomTable();
    })
  },
  viewTable(e) {
    let item = e.detail;
    this.setData({
      viewCourse: item,
      viewShow: true
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(app.globalData.data)
    emitter.on('app-reload', res => {
      this.setData({
        startDate: app.globalData.startDate
      })
    })
    if (app.globalData.startDate != undefined) {
      this.setData({
        startDate: app.globalData.startDate
      })
    }
  },
  /**
   * 课表查询
   */
  queryTimeTable(params) {
    if (params == undefined) {
      params = {};
    }
    console.log(params)
    wx.showLoading({
      title: '查询中',
    })
    wx.cloud.callFunction({
      name: 'core-table-api',
      data: {
        action: 'getTable',
        params: params
      },
      success: res => {
        if (res.result.code == 200) {
          if (res.result.data.userInfo != null) {

            let tableData = buildDayTable(res.result.data.tableData);
            console.log(tableData)
            this.setData({
              courseData: this.data.courseData.concat(tableData)
            })
          } else {
            console.warn('用户未绑定教务系统')
          }
        }
      },
      complete: res => {
        console.log(res)
        wx.hideLoading();
      }
    })
  },
  /**自定义课表查询 */
  queryCustomTable() {
    plugin.getCustomTable({
      userId: this.data.userId,
      devMode,
      appId,
      weeks: this.data.week
    }).then(res => {
      //console.log(devMode)
      console.log(res)
      this.setData({
        courseData: this.data.courseData.concat(res.data)
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.queryTimeTable();
    this.queryCustomTable();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      userId: app.globalData.openid
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})